from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
##########
from selenium import webdriver
from pyvirtualdisplay import Display
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import sys
import os
import subprocess
import time
##########
def getlogin(request):
        return render(request,'login.html')

def getindex(request):
    if request.method == 'GET' :
        return render(request,'index.html')
    else:
        if request.POST["username"] == "admin" and request.POST["password"] == "123" :
            return render(request,'index.html')
        else :
            return HttpResponse("用户名或密码错误！")
################ Reserve ###############20200903
@csrf_exempt
def getReserve(request):
    if request.method == 'GET':
        return render(request,'pay.html')
    else:
        if request.POST["user"] != "" and request.POST["pwd"] !="" :
            #Reserve Process
            usr = request.POST["user"]
            pwd = request.POST["pwd"]
            curr_hour = int(time.strftime('%H',time.localtime()))
            if curr_hour>=22:
                _flag = 0
            else:
                _flag = 1

            if _flag == 1:
                try:
                    return HttpResponse('该接口暂时不可用')
                    display = Display(visible=0, size=(1366,768))

                    display.start()
                    binary = FirefoxBinary("/opt/firefox/firefox") 
                    #status, output = subprocess.getstatusoutput("echo changshijie123|sudo -S /usr/bin/python3.6 /opt/bro.py") 
                    #browser = output
                    
                    browser = webdriver.Firefox(firefox_binary=binary,log_path="/tmp/geckodriver.log")
                    browser.get('http://my.lzu.edu.cn:8080/login?service=http%3A%2F%2Fseat.lib.lzu.edu.cn%2Fcas%2Findex.php%3Fcallback%3Dhttp%3A%2F%2Fseat.lib.lzu.edu.cn%2Fhome%2Fbook%2Fmore%2Flib%2F12%2Ftype%2F4')
                    
                    input_box_usr = browser.find_element_by_id('username')
                    input_box_pwd = browser.find_element_by_id('password')
                    input_box_usr.send_keys(usr)
                    input_box_pwd.send_keys(pwd)

                    button = browser.find_element_by_xpath('//*[@id="loginForm"]/div[4]/button')
                    button.click()
                    time.sleep(3)
                    browser.execute_script('document.querySelector("body > div.col-xs-12.col-sm-9.xiaoqu > div.x_panel.lists > div.panel.visible-md.visible-lg > div.panel-body > div:nth-child(4) > a").click()')
                    time.sleep(1)
                    browser.execute_script('document.getElementById("signUp").click()')
                    time.sleep(1)
                    browser.find_element_by_xpath('/html/body/div[8]/div/table/tbody/tr[3]/td/div[2]/button[2]').click()
                    time.sleep(1)
                    browser.quit()
                    display.stop()
                    return HttpResponse('预约成功，请在半小时内到馆。')
                except Exception as e:
                    browser.quit()
                    display.stop()
                    return HttpResponse('预约出错，请联系系统管理员。')
            else:
                return HttpResponse('未在预约时间段。')
        else :
            return HttpResponse("用户名或密码不能为空！谢谢。")

# Create your views here.
